/**
 * Copyright (c) 2012-2014 All Rights Reserved.
 */
package com.easy.framwork.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * once filter
 * 
 * @author wy
 * @version v 0.1 2014-1-3 下午10:56:58 wy Exp $
 */
public abstract class OncePerRequestFilter implements Filter {

    /** filter flag suffix */
    public static final String ALREADY_FILTERED_SUFFIX = ".FILTERED";

    /** filter config */
    protected FilterConfig filterConfig;

    /**
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    @Override
    public final void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        otherInit();
    }

    /**
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public final void doFilter(ServletRequest request, ServletResponse response,
                               FilterChain filterChain) throws ServletException, IOException {

        if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
            throw new ServletException("OncePerRequestFilter just supports HTTP requests");
        }
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
        if (request.getAttribute(alreadyFilteredAttributeName) != null
            || shouldNotFilter(httpRequest)) {
            filterChain.doFilter(request, response);
        } else {
            request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
            try {
                doFilterInternal(httpRequest, httpResponse, filterChain);
            }
            finally {
                request.removeAttribute(alreadyFilteredAttributeName);
            }
        }
    }

    /**
     * 
     * 
     * @return
     */
    protected String getAlreadyFilteredAttributeName() {
        return getFilterName() + ALREADY_FILTERED_SUFFIX;
    }

    /**
     * get filter name
     * 
     * @return
     */
    protected final String getFilterName() {
        return (this.filterConfig != null ? this.filterConfig.getFilterName() : getClass()
            .getName());
    }

    /**
     * 
     * 
     * @param request
     * @return
     * @throws ServletException
     */
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
        return false;
    }

    /**
     * Same contract as for <code>doFilter</code>, but guaranteed to be just
     * invoked once per request. Provides HttpServletRequest and
     * HttpServletResponse arguments instead of the default ServletRequest and
     * ServletResponse ones.
     */
    protected abstract void doFilterInternal(HttpServletRequest request,
                                             HttpServletResponse response, FilterChain filterChain)
                                                                                                   throws ServletException,
                                                                                                   IOException;

    /**
     * init other
     * 
     * @throws ServletException
     */
    protected void otherInit() throws ServletException {

    }

    /**
     * @see javax.servlet.Filter#destroy()
     */
    @Override
    public void destroy() {

    }

}
